/*
 * QNoteListView.h
 *
 *  Created on: Jul 1, 2010
 *      Author: alex
 */

#ifndef QNOTELISTVIEW_H_
#define QNOTELISTVIEW_H_

#include "QNoteListItemWidget.h"
#include <MList>
#include "QAbstractNoteView.h"
#include "QvernoteAPI.h"
#include "QNotesView.h"
#include <QListWidgetItem>
#include "QNoteListItemModel.h"

class QNoteListView : public MList,  public QAbstractNoteView
{
	Q_OBJECT

public:
        QNoteListView(MWidget* parent = 0)
        : MList(parent), m_currentRowIndex(0) {
		/*setFrameShape(QFrame::NoFrame);
		setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		setAutoScroll(false);*/
                setCellCreator(new QNoteListItemWidget());
                setItemModel(new QNoteListItemModel());

                QObject::connect(this, SIGNAL(itemLongTapped(const QModelIndex &)), SLOT(selectNote(const QModelIndex &)));
		QObject::connect(this, SIGNAL(noteSelected(Note&)), dynamic_cast<QNotesView*>(parentWidget()), SLOT(openNoteViewer(Note&)));
		QObject::connect(this, SIGNAL(noteLoadDone()), dynamic_cast<QNotesView*>(parentWidget()), SLOT(onNoteLoadDone()));
	}

	void redrawNotes(QString filterString = "") {
		setVisible(false);
                setItemModel(new QNoteListItemModel(filterString));
		setVisible(true);
		emit noteLoadDone();
	}

        bool nextNote(Note& nextNote) {
		m_currentRowIndex++;
                QModelIndex nextIndex = itemModel()->index(m_currentRowIndex, 0);
		if(nextIndex.isValid())
		{
                        selectItem(nextIndex);
			QVariant data = nextIndex.data(Qt::DisplayRole);
			int noteptr = data.value<int>();
			nextNote = *(Note*)(int*)noteptr;
			return true;
		}

                m_currentRowIndex--;
		return false;
	}

	bool prevNote(Note& prevNote) {
		m_currentRowIndex--;
                QModelIndex prevIndex = itemModel()->index(m_currentRowIndex, 0);
		if(prevIndex.isValid())
		{
                        selectItem(prevIndex);
                        QVariant data = prevIndex.data(Qt::DisplayRole);
			int noteptr = data.value<int>();
			prevNote = *(Note*)(int*)noteptr;
			return true;
		}

                m_currentRowIndex++;
		return false;
        }

public slots:
	void selectNote(const QModelIndex& index) {
                emit noteSelected(((QNoteListItemModel*)itemModel())->getNote(index));
	}

signals:
	void noteSelected(Note&);
	void noteLoadDone();

private:
	vector<Note> filteredNoteList;
	int m_currentRowIndex;
};
#endif /* QNOTELISTVIEW_H_ */
